package com.game.caro.data;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.mina.core.session.IoSession;

import com.game.caro.server.ReceiveCommand;
import com.game.caro.server.Server;

public class Player 
{
	private IoSession ioSession;
	private int id;
	private String username;
	private String name;
	private boolean gender;
	private int age;
	private String password;
	
	/**
	 * Trang thai nguoi choi:
	 * 			0: ko dang choi
	 * 			1: dang choi
	 */
	private int status;
	
	/**
	 * Kiem tra den luot choi hay chua
	 */
	private boolean isTurn;
	
	private int win;
	private int lose;
	private int dissolve;
	
	/** ban choi hien tai */
	private PlayTable currentPlayTable;
	
	/** Mang luu cac o nguoi choi da tick */
	private ArrayList<Square> squares = new ArrayList<Square>();
	

	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setUsername(String username) 
	{
		this.username = username;
	}
	
	public String getUsername() 
	{
		return username;
	}
	
	public void setGender(boolean gender)
	{
		this.gender = gender;
	}
	
	public boolean getGender() 
	{
		return gender;
	}
	
	public void setAge(int age)
	{
		this.age = age;
	}
	
	public int getAge() 
	{
		return age;
	}
	
	public void setStatus(int status) 
	{
		this.status = status;
	}
	/**
	 * @return the status
	 */
	public int getStatus()
	{
		return status;
	}
	/**
	 * @param win the win to set
	 */
	public void setWin(int win) 
	{
		this.win = win;
	}
	/**
	 * @return the win
	 */
	public int getWin() 
	{
		return win;
	}
	/**
	 * @param lose the lose to set
	 */
	public void setLose(int lose) 
	{
		this.lose = lose;
	}
	/**
	 * @return the lose
	 */
	public int getLose() 
	{
		return lose;
	}
	/**
	 * @param dissolve the dissolve to set
	 */
	public void setDissolve(int dissolve)
	{
		this.dissolve = dissolve;
	}
	/**
	 * @return the dissolve
	 */
	public int getDissolve() 
	{
		return dissolve;
	}
	
	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the password
	 */
	public String getPassword() 
	{
		return password;
	}
	
	
	public IoSession getIoSession() 
	{
		return ioSession;
	}

	public void setIoSession(IoSession ioSession) 
	{
		this.ioSession = ioSession;
	}
	
	public PlayTable getCurrentPlayTable() 
	{
		return currentPlayTable;
	}

	public void setCurrentPlayTable(PlayTable currentPlayTable) 
	{
		this.currentPlayTable = currentPlayTable;
	}

	public Player(IoSession ioSession)
	{
		this.ioSession = ioSession;
	}
	public Player(String _username,boolean _gender,int _age,int _status,int _win,int _lose,int _dissolve)
	{
		this.username=_username;
		this.gender=_gender;
		this.age=_age;
		this.status=_status;
		this.win=_win;
		this.lose=_lose;
		this.dissolve=_dissolve;		
	}

	/**
	 * Ham xu ly cac message ma ioSession tuong ung voi player nay nhan duoc
	 * vd: client gui len msg = LOGIN|van|1
	 * theCommand = LOGIN
	 * theMessage = van|1
	 */
	public void messageReceived(String msg)
	{		
		String command = msg.substring(0, msg.indexOf("\\|"));
		ReceiveCommand theCommand=ReceiveCommand.valueOf(command);// theCommand la doan chuoi dau tien , mo ta loai yeu cau cua client
		String theMessage = msg.substring(msg.indexOf("\\|") + 1);// theMessage la doan chuoi con lai sau khi bo di phan command, ok?
		switch(theCommand.toInt())
		{
			case ReceiveCommand.QUIT:
			{
				this.ioSession.write("QUIT OK");
				ioSession.close(true);
				break;
			}
			case ReceiveCommand.LOGIN:
			{
				this.login(theMessage);
				break;
			
			}
			
		}
	}
	
	/**
	 * Cac ham phuc vu cho ham messageReceived
	 */
	
	/**
	 * @format LOGIN| username | password
	 */
	private void login(String theMessage)
	{		
		String[] arr = theMessage.split("\\|");
		String _username = arr[0];
		String _password = arr[1];
		boolean result = Server.databaseAccess.login(this, _username, _password);
		
		if(result = true)
		{
			this.sendMessageToMe("LOGIN| OK");//chua ok lam, chau kiem tra co thang da login roi	
		}
		else
		{
			this.sendMessageToMe("LOGIN | ERROR");
		}
	}
	
	/**
	 *ham gui message toi player hien tai
	 *
	 */
	private void sendMessageToMe(String message)
	{
		this.ioSession.write(message);
	}

}
